#!/bin/bash

red='\033[31m'
green='\033[32m'
yellow='\033[33m'
blue='\033[34m'
plain='\033[0m'
info='\033[32m'
error='\033[41;30m'
success='\033[42;30m'

echo -e "
######################################################################
#               ${red} Centos7 install kubernetes ${plain}
#${blue} Version: docker-ce-20.10.15 & kubelet-1.23.0 ${plain}
#${yellow} Notices: Advised to use it in a test environment. ${plain}
#${yellow}          Yum update your server at first would be helpful. ${plain}
#${yellow}          Setting static IP adderess and the same root password for each host. ${plain}
#${yellow}          Just need to run the script on the master. ${plain}
#${green} Reference: ①https://gitee.com/guoshaosong/scripts/blob/master/k8s(yuan).sh ${plain}
#${green}            ②https://www.cnblogs.com/hei-hei-hei/p/13803806.html ${plain}
#${green}            ③https://www.cnblogs.com/fengzi7314/p/12444762.html ${plain}
# 2022-05-05
######################################################################
"

master_name=k8s-master
node_name=k8s-node

# 获取节点ip信息
master_ip=`ip a |grep -A2 ^...e|grep inet |awk '{ print $ 2}'|sed 's/...$//'`
echo "The current server:$master_ip will be master."
read -ep "Enter node IP address separated by Spaces:" input_ip
read -esp "Enter root password:" input_password
node_ip=($input_ip)
export password=$input_password
export pwd=`pwd`

echo
echo "------------------------"
echo master_ip:$master_ip
echo node_ip:${node_ip[@]}
echo "------------------------"

download_files(){
# 下载需要文件
echo -e "${info} [1/6] Download files... ${plain}"
yum install -y wget curl &>/dev/null
mkdir $pwd/k8s
cd $pwd/k8s
wget --no-check-certificate https://gitee.com/zhongxiongjun/bash_scripts/raw/master/k8s/common.sh &>/dev/null
wget --no-check-certificate https://gitee.com/zhongxiongjun/bash_scripts/raw/master/k8s/ssh-auth.sh &>/dev/null
wget --no-check-certificate https://gitee.com/zhongxiongjun/bash_scripts/raw/master/k8s/dashboard-conf.sh &>/dev/null
#wget --no-check-certificate https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml &>/dev/null
#wget --no-check-certificate https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.1/aio/deploy/recommended.yaml &>/dev/null
wget --no-check-certificate https://gitee.com/zhongxiongjun/bash_scripts/raw/master/k8s/kube-flannel.yml &>/dev/null
wget --no-check-certificate https://gitee.com/zhongxiongjun/bash_scripts/raw/master/k8s/recommended.yaml &>/dev/null
wget --no-check-certificate https://gitee.com/zhongxiongjun/bash_scripts/raw/master/k8s/dashboard-adminuser.yaml  &>/dev/null
cd $pwd

# 检查文件是否下载成功
if [ ! -f "$pwd/k8s/common.sh" ] || [ ! -f "$pwd/k8s/ssh-auth.sh" ] || [ ! -f "$pwd/k8s/kube-flannel.yml" ] || [ ! -f "$pwd/k8s/recommended.yaml" ]; then
	echo -e "${error} [Error]: File download failed! ${plain}"
    exit 1
fi
}

hosts_template(){
# 生成hosts文件模板
echo -e "${info} [2/6] Generate the hosts.template... ${plain}"
echo "$master_ip $master_name" >> $pwd/k8s/hosts.template
for (( i = 0; i < ${#node_ip[@]}; i++))
do
  ipaddres=${node_ip[$i]}
  hostname=$node_name$[i+1]
  echo "$ipaddres $hostname" >> $pwd/k8s/hosts.template
done

# 执行集群免密脚本
sh $pwd/k8s/ssh-auth.sh
}

master_install(){
# master节点部署
echo -e "${info} [3/6] Deploy master... ${plain}"
sh $pwd/k8s/common.sh
kubeadm init \
	--image-repository registry.aliyuncs.com/google_containers \
	--apiserver-advertise-address $master_ip \
	--pod-network-cidr=10.244.0.0/16 \
	--service-cidr=10.1.0.0/16 \
	--token-ttl 0  &>/dev/null

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

kubeadm token create --print-join-command > $pwd/k8s/join-command.sh
}

nodes_install(){
# 远程登录到node节点部署
echo -e "${info} [4/6] Deploy node... ${plain}"
for node in ${node_ip[@]}
do
echo -e "${info} node_ip:$node ${plain}"
scp $pwd/k8s/common.sh root@$node:/root/common.sh &>/dev/null
scp $pwd/k8s/join-command.sh root@$node:/root/join-command.sh &>/dev/null
ssh -T root@$node <<EOF
sh /root/common.sh && sh /root/join-command.sh &>/dev/null
rm -rf /root/common.sh
rm -rf /root/join-command.sh
exit
EOF
done
}

deploy_flannel(){
echo -e "${info} [5/6] Deploy flannel... ${plain}"
kubectl apply -f $pwd/k8s/kube-flannel.yml  &>/dev/null
}

deploy_dashboard(){
echo -e "${info} [6/6] Deploy Dashboard... ${plain}"
kubectl apply -f $pwd/k8s/recommended.yaml  &>/dev/null

echo -e "${success} [INFO] Success! Enjoy it! ${plain}"
echo -e "${red} To configure Dashboard, just waiting for the Dashboard pod running, and then execute following command: ${plain}"
echo -e "${yellow} cd $pwd/k8s && sh dashboard-conf.sh ${plain}"
}

download_files
hosts_template
master_install
nodes_install
deploy_flannel
deploy_dashboard